﻿using System;
using System.Threading;
using XcoAppSpaces.Contracts.Logging;
using XcoAppSpaces.Transport.WCF;
using NUnit.Framework;
using XcoAppSpaces.Contracts.Transport;
using XcoAppSpaces.Serialization.Binary;

namespace test.XcoAppSpaces.Transport.WCF.nettcp
{
    [TestFixture]
    public class testXcoWCFTransportService
    {


        private ReceivedTmp recv1 = new ReceivedTmp();
        private ReceivedTmp recv2 = new ReceivedTmp();
        private ManualResetEvent waitForStop1;
        private ManualResetEvent waitForStop2;
        private XcoBinarySerializer serializer = new XcoBinarySerializer();

       



        /*	[Test]
            public void Startup()
            {
                using (XcoWCFTransportService wcf = new XcoWCFTransportService("net.pipe://localhost/XcoAppSpace"))
                {
                    //wcf.SetLogger(new TestLoggerFactory());
                    wcf.Start(new MessageReceivedMethod(MessageReceivedTest1), null);
                    Assert.IsTrue(wcf.Running);
                    wcf.Stop();
                    Assert.IsFalse(wcf.Running);
                }
            }*/
        [Test]
        public void Connect()
		{
			waitForStop1 = new ManualResetEvent(false);
			waitForStop2 = new ManualResetEvent(false);

            string netpipe1 = "net.pipe://localhost/XcoAppSpace1";
            string netpipe2 = "net.pipe://localhost/XcoAppSpace2";
            using (XcoWCFTransportService wcf1 = new XcoWCFTransportService(netpipe1))
			{
                using (XcoWCFTransportService wcf2 = new XcoWCFTransportService(netpipe2))
				{
					wcf1.OnMessageReceived += recv1.MessageReceived;
					wcf2.OnMessageReceived += recv2.MessageReceived;
					
					wcf1.Start();
					wcf2.Start();

					byte[] data = serializer.Serialize(new IntMsg {value = 5});
					byte[] data2 = serializer.Serialize(new IntMsg {value = 3});

					wcf2.Send(new XcoMessage { Content = data }, wcf1.LocalAddress);
                    wcf1.Send(new XcoMessage { Content = data2 }, wcf2.LocalAddress);

					waitForStop1.WaitOne(1000);
					waitForStop2.WaitOne(1000);

					wcf1.Stop();
					wcf2.Stop();
                    Assert.AreEqual(recv1.Sum, 5);
                    Assert.AreEqual(recv2.Sum, 3);
					
				}
			}
		}

    }

   
}